<?php

namespace App\Models;

use Zizaco\Entrust\EntrustPermission;

class Permission extends EntrustPermission
{
    const SERVER_TYPE_DEV     = 'dev';
    const SERVER_TYPE_STAGING = 'staging';
    const SERVER_TYPE_PRD     = 'prd';

    protected $guarded = [];

    public static function getDeployPermissionName($project_id, $setting_id)
    {
        $permission_name = 'deploy_' . $project_id . '_' . $setting_id;
        if (!self::where('name', $permission_name)->exists()) {
            $role_name = Role::getDeployRoleName($project_id, $setting_id);

            $permission = self::create([
                'name'         => $permission_name,
                'display_name' => $permission_name,
                'description'  => 'project_id = ' . $project_id . ', setting_id = ' . $setting_id . ' 的权限（自动生成）',
            ]);

            $role = Role::whereName($role_name)->first();

            $role->attachPermission($permission);
        }

        return $permission_name;
    }
}
